package com.wdh.mytabdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.Manifest;
import android.os.Bundle;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;


@RuntimePermissions
public class MainActivity extends AppCompatActivity {

    CommonTabLayout commontab;
    private String[] mTitles = {"首页", "预测", "赛事", "任务", "我的"};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private int[] mIconUnselectIds = {
            R.mipmap.main_tab_un, R.mipmap.main_tab_un1, R.mipmap.main_tab_un2, R.mipmap.main_tab_un4, R.mipmap.main_tab_un3};
    private int[] mIconSelectIds = {
            R.mipmap.main_tab_s, R.mipmap.main_tab_s1, R.mipmap.main_tab_s2, R.mipmap.main_tab_s4, R.mipmap.main_tab_s3};

    //public static boolean isForeground = false;
    // private static PushHandler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        commontab = findViewById(R.id.commontab);
        MainActivityPermissionsDispatcher.needsWithPermissionCheck(this);
        initTab();
    }

    private void initTab() {
        ArrayList<Fragment> mFragments = new ArrayList<>();
        mFragments.clear();
        HomeFragment homeFrgment = new HomeFragment();
        mFragments.add(homeFrgment);
        TwoFragment twoFrgment = new TwoFragment();
        mFragments.add(twoFrgment);
        HomeFragment homeFrgment2 = new HomeFragment();
        mFragments.add(homeFrgment2);
        TwoFragment twoFrgment2 = new TwoFragment();
        mFragments.add(twoFrgment2);
        HomeFragment homeFrgment3 = new HomeFragment();
        mFragments.add(homeFrgment3);
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        commontab.setTabData(mTabEntities, this, R.id.main_container, mFragments);
        commontab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                commontab.setCurrentTab(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        commontab.setCurrentTab(0);
    }


    @NeedsPermission(Manifest.permission.ACCESS_FINE_LOCATION)
    void needs() {
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }
}
